import {
  IsArray,
  IsEmail,
  IsEnum,
  IsInt,
  IsMobilePhone,
  IsString,
  Max,
  Min,
} from 'class-validator';
import { RoleType } from 'src/common/constant';
import { createUUID } from 'src/utils/tools';
import {
  Column,
  CreateDateColumn,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  OneToOne,
  PrimaryColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Roles } from './roles.entity';

@Entity({ comment: '用户信息表' })
export class Users {
  // 手动管理主键
  // @PrimaryGeneratedColumn() // 自增整数主键,会导致数据库中的 id 类型变为 int，但是可以这样 @PrimaryGeneratedColumn('uuid') // 自动生成 UUID 主键
  constructor() {
    this.id = createUUID();
  }

  @PrimaryColumn('varchar', { length: 32 })
  id: string;

  @Column({ length: 50, comment: '名字' })
  @IsString()
  name: string;

  @Column({ comment: '年龄' })
  @IsInt()
  @Min(1)
  @Max(100)
  age: number;

  @Column({ type: 'simple-array', comment: '兴趣爱好' })
  @IsArray()
  hobby: string[];

  @Column({ comment: '描述' })
  @IsString()
  description: string;

  @Column({ length: 20, comment: '电话号码' })
  @IsMobilePhone()
  phone: string;

  @Column({ length: 50, comment: '邮箱' })
  @IsEmail()
  email: string;

  @Column({ length: 50, comment: '密码', default: '123456' })
  password: string;

  @Column({ type: 'enum', enum: [0, 1], default: 1, comment: '是否激活用户' })
  is_Active: [0, 1];

  @CreateDateColumn({ name: 'created_at', comment: '创建时间' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', comment: '更新时间' })
  updatedAt: Date;

  @ManyToMany(() => Roles, (roles) => roles.users)
  @JoinTable({ name: 'users_roles' })
  roles: Roles[];
}
